package com.cloudminds.resttest.mediatester;

import com.cloudminds.resttest.api.CallManager;
import com.cloudminds.resttest.service.NewWebSocketChannelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MediaTesterJni {
    private static Logger logger = LoggerFactory.getLogger(MediaTesterJni.class);

    public interface MediaTesterEvent{
        public void onIceCandidate(final String candidate);
    }

    private MediaTesterEvent mediaTesterEvent;
    public native long CreateMediaTester(boolean publisher, String samplefile);
    public native void ReleaseMediaTester(long tester);
    public native String GenerateSDP(long tester);
    public native void ProcessSDP(long tester, String sdp);

    public void setMediaTesterEvent(MediaTesterEvent mediaTesterEvent) {
        this.mediaTesterEvent = mediaTesterEvent;
    }

    private void onCandidateCallBack(long tester, boolean done, String candidate) {
        if (!done) {
            System.out.println("["+ tester + "]candidate:" + candidate + "\n");
        }
        else {
            System.out.println("["+ tester + "]candidate:done\n");
        }
        if (mediaTesterEvent != null){
            mediaTesterEvent.onIceCandidate(candidate);
        }
    }
    private void onIceStatusCallBack(long tester, int state) {
        /* 枚举
         NICE_COMPONENT_STATE_DISCONNECTED  No activity scheduled
         NICE_COMPONENT_STATE_GATHERING     Gathering local candidates
         NICE_COMPONENT_STATE_CONNECTING    Establishing connectivity
         NICE_COMPONENT_STATE_CONNECTED     At least one working candidate pair
         NICE_COMPONENT_STATE_READY         ICE concluded, candidate pair selection is now final
         NICE_COMPONENT_STATE_FAILED        Connectivity checks have been completed, but connectivity was not established
         NICE_COMPONENT_STATE_LAST          Dummy state
         */
        System.out.println("["+ tester +"]Ice Status:" + state);
    }

    static {

        //System.out.println(System.getProperties().get("java.library.path"));
        try {
            System.loadLibrary("mediatester");
            logger.info("loadLibrary Success。。。。");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
//    public static void main(String[] args) {
//        MediaTesterJni mediatester = new MediaTesterJni();
//        long tester = mediatester.CreateMediaTester(true,"/workspace/trunk/develop/cloudminds/libmediatester/rec-sample-video.mjr");
//        String sdp = mediatester.GenerateSDP(tester);
//        System.out.print("["+ tester +"]SDP:" + sdp);
//
//        while (true) {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        //mediatester.ReleaseMediaTester(tester);
//    }
}
